package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategyComment")
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService strategyCommentService;
    //获取所有数据并共享给页面
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyCommentQuery qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "/strategyComment/list";
    }
    //保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyComment strategyComment){
        if(StringUtils.hasLength(strategyComment.getId())){
            strategyCommentService.save(strategyComment);
        }else {
            strategyComment.setId(null);
            strategyCommentService.save(strategyComment);
        }
        return JsonResult.success();
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        strategyCommentService.delete(id);

        return  JsonResult.success();
    }
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){
        String content = strategyCommentService.get(id).getContent();
        return JsonResult.success(content);
    }
}
